package com.example.enterprisemanager.mvvm.fragment.allocate;

import com.example.enterprisemanager.base.BaseModel;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.AllocateGoods;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.EnrollGoods;
import com.orhanobut.logger.Logger;

import java.util.List;

import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class AllocateModel extends BaseModel {
    private Disposable mGetHaveDisposable;
    private Disposable mUpdateDepartDisposable;
    private Disposable mHaveAllocateNumDisposable;
    private Disposable mHaveAllocateDisposable;


    public void getCanBeAllocated(Consumer<List<EnrollGoods>> success, Consumer<Exception> fail) {
        BmobQuery<EnrollGoods> query = new BmobQuery<>();

        mGetHaveDisposable = query.findObjects(new FindListener<EnrollGoods>() {
            @Override
            public void done(List<EnrollGoods> list, BmobException e) {
                try {
                    if (e == null) {
                        success.accept(list);
                    } else {
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });

    }

    public void updateGoodsDepart(List<BmobObject> goods, Consumer<List<BatchResult>> success, Consumer<Exception> fail) {

        mUpdateDepartDisposable = new BmobBatch().updateBatch(goods).doBatch(new QueryListListener<BatchResult>() {
            @Override
            public void done(List<BatchResult> results, BmobException e) {
                try {

                    if (e == null) {
                        for (int i = 0; i < results.size(); i++) {
                            BatchResult result = results.get(i);
                            BmobException ex = result.getError();
                            if (ex == null) {
                                Logger.d("第" + i + "个数据批量更新成功：" + result.getCreatedAt() + "," + result.getObjectId() + "," + result.getUpdatedAt());
                            } else {
                                Logger.d("第" + i + "个数据批量更新失败：" + ex.getMessage() + "," + ex.getErrorCode());

                            }
                        }
                        success.accept(results);
                    } else {
                        Logger.d("失败：" + e.getMessage() + "," + e.getErrorCode());
                        fail.accept(e);
                    }

                } catch (Exception e1) {
                    Logger.d(e1.getMessage());
                }
            }
        });

    }

    public void haveAllocateUpdateNum(List<BmobObject> goods, EnrollGoods enrollGoods, Consumer<List<BatchResult>> success, Consumer<Exception> fail) {
        mHaveAllocateNumDisposable = enrollGoods.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                try {
                    if (e == null) {
//                        success.run();
                        updateGoodsDepart(goods, success, fail);
                    } else {
//                        fail.accept(e);

                    }
                } catch (Exception e1) {

                }
            }
        });
    }

    public void haveAllocate(EnrollGoods enrollGoods, int num, List<String> ids, String department, String username, Action success, Consumer<Exception> fail) {
        AllocateGoods allocateGoods = new AllocateGoods(enrollGoods.getName(), enrollGoods.getCompany(), enrollGoods.getAvatar()
                , enrollGoods.getPrice(), enrollGoods.getType(), ids, department, username, num, num, 0);
        mHaveAllocateDisposable = allocateGoods.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                try {
                    if (e == null) {
                        Logger.d("新增成功：");
                        success.run();
                    } else {
                        Logger.d(e.getMessage());
                        fail.accept(e);
                    }
                } catch (Exception e1) {

                }
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mGetHaveDisposable != null) {
            mGetHaveDisposable.dispose();
            mGetHaveDisposable = null;
        }
        if (mUpdateDepartDisposable != null) {
            mUpdateDepartDisposable.dispose();
            mUpdateDepartDisposable = null;
        }
        if (mHaveAllocateNumDisposable != null) {
            mHaveAllocateNumDisposable.dispose();
            mHaveAllocateNumDisposable = null;
        }
        if (mHaveAllocateDisposable != null) {
            mHaveAllocateDisposable.dispose();
            mHaveAllocateDisposable = null;
        }
    }
}
